<?php

/**
 * 数据类
 */
class Data {

	// 目前录入数据库的分类,不区分大小写
	public $catalogs = array(
		"tuigirl" => array("key" => "TuiGirl", "name" => "推女郎", "logo" => "/images/logo/tuigirl.jpg", ), 
		"youguowang" => array("key" => "youguowang", "name" => "尤果网", "logo" => "/images/logo/youguowang.png", ), 
		"mygirl" => array("key" => "Mygirl", "name" => "Mygirl美媛馆", "logo" => "/images/logo/mygirl.jpg", ), 
		"tgod" => array("key" => "TGOD", "name" => "TGOD推女神", "logo" => "/images/logo/tgod.png", ), 
		"3agirl" => array("key" => "3Agirl", "name" => "3Agirl", "logo" => "/images/logo/3agirl.png", ), 
		"xiuren" => array("key" => "xiuren", "name" => "秀人网", "logo" => "/images/logo/xiuren.png", )
	);
	/**
	 * 获取每个分类的几张新图作为slider的封面图
	 */
	public function getSlider() {
		$ret = array();
		foreach ($this->catalogs as $key => $value) {
			$slide = $this->find($key);
			if( $slide ){
				$ret[] = $slide;				
			}
		}
		ajaxReturn($ret, "success", 1);
	}

	public function getCatalog() {
		ajaxReturn($this -> catalogs, "success", 1);
	}

	public function add() {
		$url = I("url");
		$name = I("name", "");

		$time = date("YmdHis");
		$data = array("name" => $name, "url" => $url, "time" => $time);
		$field = "add";
		$redis = MR();
		$ret = $redis -> lpush($field, $data);
		ajaxReturn($ret, "success", 1);

	}

	/**
	 * 获取一页数据
	 */
	public function get() {
		$page = I("p", 0);
		$field = I("f", "tuigirl");
		$page_cnt = 6 ;

		$field = $this -> getKey($field);
		if (empty($this -> catalogs[$field])) {
			ajaxReturn(null, "error", 0);
		}
		$redis = MR();
		$list = $redis -> lrange($field, $page * $page_cnt, (($page + 1) * $page_cnt) - 1);
		ajaxReturn($list, "success", 1);
	}

	/**
	 * 添加数据
	 */
	public function set() {
		$field = I("f");
		if (empty($this -> catalogs[$field])) {
			ajaxReturn(null, "field error!", 0);
		}
		$field = $this -> getKey($field);
		$input = "data/" . $field . ".txt";
		if (file_exists($input)) {
			$lines = file_get_contents($input);
			$list = explode("\r\n", $lines);
			$ret = $this -> save($field, $list);
			ajaxReturn(array("cnt" => $ret), "finish", 1);
		}else{
			ajaxReturn(null,"error",0);
		}
	}
	
	/**
	 * 全部设置一遍
	 */
	public function setAll(){
		$cnt = 0 ;
		foreach ($this->catalogs as $key => $value) {
			$field = $this -> getKey($key);
			$input = "data/" . $field . ".txt";
			if (file_exists($input)) {
				$lines = file_get_contents($input);
				$list = explode("\r\n", $lines);
				$cnt += $this -> save($field, $list);
			}
		}
		ajaxReturn(array("cnt" => $cnt),"finish",1);
	}

	private function getKey($field) {
		return strtolower($field);
	}

	public function clear() {
		$field = I("f");
		$field = $this -> getKey($field);
		$redis = MR();
		if( $field == "all" ){
			$ret = $redis -> flushdb();
		}else{
			$ret = $redis -> del($field);
		}
		ajaxReturn($ret, "delete {$field} success", 1);
	}
	
	/**
	 * 查看redis信息
	 */
	public function info(){
		$redis = MR();
		$info = $redis->info();
		var_dump($info);
	}

	/**
	 * 持久化
	 */
	public function save($field, $list, $perm = false) {
		// 保存到mysql
		if ($perm) {
			return false;
		} else {
			$redis = MR();
			foreach ($list as $value) {
				$ret = $redis -> rpush($field, $value);
			}
			return $ret ; 
		}
	}

	/**
	 * 把文件下载到本地
	 * 返回成功保存数
	 */
	public function download($list, $saveTo) {
		$cnt = 0;
		foreach ($list as $key => $value) {
			$savePath = $saveTo . basename($value);
			if (file_put_contents($savePath, file_get_contents($value)) > 0) {
				$cnt++;
			}
		}
		return $cnt;

	}

	/**
	 * 只找一张
	 */
	public function find($key) {
		$redis = MR();
		return $redis -> lindex($key, 0);
	}

}
